package com.usian.controller;

import com.usian.CartFeign;
import com.usian.Result;
import com.usian.feign.OrderFeign;
import com.usian.pojo.OrderInfo;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbOrder;
import com.usian.pojo.TbOrderShipping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

@RequestMapping("/frontend/order")
@RestController
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private CartFeign cartFeign;

    @RequestMapping("/goSettlement")
    public Result goSettlement(String[] ids,String userId,String token){
//        从redis中取用户的购物车信息
        Map<String, TbItem> tbItemMap = cartFeign.selectCartByUserId(userId);
//        从购物车中获取选中的商品
        ArrayList<TbItem> tbItems = new ArrayList<>();
        for (String id : ids) {
            tbItems.add(tbItemMap.get(id));
        }
        if (tbItems.size()>0){
            return Result.ok(tbItems);
        }
        return Result.error("error");
    }

    /**
     * 创建订单
     *
     * @param orderItem
     * @param tbOrder
     * @param tbOrderShipping
     * @return
     */
    @RequestMapping("/insertOrder")
    public Result insertOrder(String orderItem,
                              TbOrder tbOrder ,
                              TbOrderShipping tbOrderShipping){
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderItem(orderItem);
        orderInfo.setTbOrder(tbOrder);
        orderInfo.setTbOrderShipping(tbOrderShipping);
        Long orderId = orderFeign.insertOrder(orderInfo);
        if (orderId!=null){
            return Result.ok(orderId);
        }
        return Result.error("创建订单失败");
    }
}
